Python の super() と super (className,self) の違い (Difference between super() and super (className,self) in Python)


問題の説明

Python の super() と super (className,self) の違い (Difference between super() and super (className,self) in Python)

次のようにコードを切り取りました:

class A:
    def test(self):
        return 'A'

class B(A):
    def test(self):
        return 'B‑>' + super(B, self).test()

print(B().test())

出力: B‑>A

このように書くと、同じ出力が得られます:

class A:
    def test(self):
        return 'A'

class B(A):
    def test(self):
        return 'B‑>' + super().test()  # change to super()


print(B().test())

どちらの場合も、同じ出力が得られます。では、この 2 種類の super の呼び出しの違いは何ですか? どちらを使用する場合の長所と短所は何ですか?


リファレンスソリューション

方法 1:

In Python 2, only the super(className,self) syntax was possible. Since It was the most used version, as of Python 3 providing no arguments will act the same.

There are two typical use cases for super. In a class hierarchy with single inheritance, super can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable

(by Taohidul IslamFrenchMasterSword)

リファレンスドキュメント

  1. Difference between super() and super (className,self) in Python (CC BY‑SA 2.5/3.0/4.0)

#Python #inheritance #python-3.x #class






関連する質問

再帰的なテキスト分割の問題 (Trouble with recursive text splitting)

行継続文字エラーの後に予期しない文字が表示されます (I am getting an unexpected character after line continuation character error)

distutils で Tkinter を要求するにはどうすればよいですか? (How do I require Tkinter with distutils?)

Python の super() と super (className,self) の違い (Difference between super() and super (className,self) in Python)

TensorFlow 2はcolab googleとwindows 10でバージョンを表示しません (TensorFlow 2 not show the version in colab google and windows 10)

それぞれが親ループに依存する4つのネストされたループの時間の複雑さは何ですか? (What is time complexity of 4 nested loops which each depend on the parent loop)

Pyqt5 での KeyEvent の正しい処理、KeyPressEvent のキャッチに関する問題 (Correct handling of KeyEvent in Pyqt5, problem with catching KeyPressEvent)

文字列形式の辞書のリストを Python のデータフレームに変換する方法はありますか? (Is there a way to convert list of string formatted dictionary to a dataframe in Python?)

母音 + 周囲の子音で文字列を分割 (split string at vowel + surrounding consonants)

plumbum: stdin に変数を送信する方法は? (plumbum: How to send a variable to stdin?)

Python - ビデオ処理。方法: 1) ビデオのピクセル値を変更し (つまり、ピクセル効果)、2) すべてのピクセルの情報を取得します。 (Python - video processing. How to: 1) change pixels value in videos (ie pixelated effect), and then 2) retrieve every single pixel's information)

ボットが 1 つのコマンド discord.py に複数回応答する問題 (Issue with bot responding multiple times to one command discord.py)







コメント